<template>
  <!-- 弹出层 -->
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCancel">
    <el-form
      ref="formData"
      label-width="120px"
      :model="ruleForm"
      :rules="rules"
    >
      <el-form-item label="姓名">
        <el-input
          v-model="ruleForm.username"
          style="width: 50%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="ruleForm.mobile"
          placeholder="请输入手机号"
          style="width: 50%"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="ruleForm.timeOfEntry"
          placeholder="请选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="ruleForm.formOfEmployment" style="width: 50%" placeholder="请选择">
          <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="ruleForm.workNumber"
          placeholder="请输入工号"
          style="width: 50%"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="ruleForm.departmentName"
          placeholder="请选择部门"
          style="width: 50%"
          @focus="getDepartments"
        />
        <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="treeData"
          :props="defaultProps"
          :default-expand-all="true"
          @node-click="selectNode"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="ruleForm.correctionTime"
          style="width: 50%"
          placeholder="请选择日期"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="btnCancel">取 消</el-button>
      <el-button type="primary" @click="btnOK">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getDepartments } from '@/api/departments'
import { addEmployee } from '@/api/employees'
import { tranListToTreeData } from '@/utils/index'
import EmployeeEnum from '@/api/constant/employees'
// import { logger } from 'runjs/lib/common'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      EmployeeEnum,
      ruleForm: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        // name: [
        //   { required: true, message: '用户姓名不能为空', trigger: 'change' },
        //   { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        // ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      },
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      loading: false,
      showTree: false
    }
  },
  methods: {
    // 取消按钮
    btnCancel() {
      // 重置表单数据
      this.ruleForm = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$refs.formData.resetFields()
      //  利用sync修饰符 关闭弹层
      this.$emit('update:showDialog', false)
    },
    // 部门 聚焦事件
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const res = await getDepartments()
      //   console.log(res)
      //   将获取回来的数据转换为 树形结构
      this.treeData = tranListToTreeData(res.depts, '')
      this.loading = false
    },
    // 给 部门input 框选择内容
    selectNode(node) {
      // 回调函数里面的参数里面的名字给部门input框
      this.ruleForm.departmentName = node.name
      //  关闭树形组件
      this.showTree = false
      //   打印回调函数里面的参数
      //   console.log(arguments)
    },
    // 点击确定
    async btnOK() {
      try {
        this.$refs.formData.validate()
        await addEmployee(this.ruleForm)
        //   console.log(res)
        this.$emit('addEmployee')
        this.$emit('update:showDialog', false)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
<style >
</style>
